package com.jiyun.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jiyun.dto.GoodsDto;
import com.jiyun.dto.R;
import com.jiyun.entity.Goods;
import com.jiyun.service.IGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.List;

/**
 * <p>
 *  控制层
 * </p>
 *
 * @author 聪哥哥
 * @since 2024-10-16
 */
@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    IGoodsService goodsService;

    @RequestMapping("/exportData")
    public void exportData(HttpServletResponse response) throws Exception{

        List<GoodsDto> list = goodsService.findList();
        response.setHeader("Content-Disposition", "attachment;filename="+ URLEncoder.encode("商品列表.xlsx", "UTF-8"));
        response.setHeader("Connection", "close");
        response.setHeader("Content-Type", "application/octet-stream");

        EasyExcel.write(response.getOutputStream())
                .head(GoodsDto.class)
                .excelType(ExcelTypeEnum.XLSX)
                .sheet(0)
                .doWrite(list);
    }

    /**
     * 添加接口
     */
    @RequestMapping("/add")
    public R addGoods(@RequestBody Goods goods){
        LambdaQueryWrapper<Goods> goodsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        goodsLambdaQueryWrapper.eq(Goods::getName,goods.getName());
        if (goodsService.count(goodsLambdaQueryWrapper)>0) {
            return R.error("商品名称重复");
        }
        goodsService.save(goods);
        return R.success();
    }

    /**
     * 修改接口
     */
    @RequestMapping("/update")
    public void updateGoods(@RequestBody Goods goods){
        goodsService.updateById(goods);
    }

    /**
     * 单个删除接口
     */
    @RequestMapping("/deleteOne")
    public void deleteOne(@RequestParam Integer id){
        goodsService.removeById(id);
    }

    /**
     * 批量删除接口
     * 前台ajax参数ids传入示例：1,2,3  (英文逗号分隔的字符串)
     */
    @RequestMapping("/deleteBatch")
    public void deleteBatch(@RequestParam List<Integer> ids){
        goodsService.removeByIds(ids);
    }

    /**
     * 根据id查询实体接口
     */
    @RequestMapping("/findById")
    public Goods findById(@RequestParam Integer id){
        return goodsService.getById(id);
    }

    /**
     * 查询所有接口
     */
    @RequestMapping("/findAll")
    public List<Goods> findAll(){
        return goodsService.list();
    }

    /**
     * 分页查询接口
     * 重要提示：启用分页功能必须在配置类中添加mybatis-plus分页的拦截器
     * 重要提示：启用分页功能必须在配置类中添加mybatis-plus分页的拦截器
     * 重要提示：启用分页功能必须在配置类中添加mybatis-plus分页的拦截器
     */
    @RequestMapping("/findPage")
    public Page<GoodsDto> findPage(@RequestParam Integer page, @RequestParam Integer pageSize, @RequestBody GoodsDto goodsDto){

        return goodsService.findPage(page,pageSize,goodsDto);
    }

}
